unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, VirtualTrees, RzButton, ExtCtrls, RzPanel,UniConfig,Uni,UniConnct,
  FormEx_View,SQLServerUniProvider,Class_Card, Grids, AdvObj, BaseGrid,
  AdvGrid, RzSplit;

type
  PCard = ^TCard;

  {TCard =record
    UnitLink:string;
    CardCode:string;
    CardName:string;
    CardZCTM:string;
    DEPTLINK:string;
  end;}

  
  TForm1 = class(TFormExView)
    Tree_View: TVirtualStringTree;
    Tool_1: TRzToolbar;
    Btnx_ListDB: TRzToolButton;
    Btnx_2: TRzToolButton;
    Btnx_3: TRzToolButton;
    RzSplitter1: TRzSplitter;
    Grid_1: TAdvStringGrid;
    Btnx_4: TRzToolButton;
    Btnx_1: TRzToolButton;
    Btnx_5: TRzToolButton;
    procedure Btnx_ListDBClick(Sender: TObject);
    procedure Btnx_2Click(Sender: TObject);
    procedure Tree_ViewGetText(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: WideString);
    procedure Tree_ViewInitNode(Sender: TBaseVirtualTree; ParentNode,
      Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
    procedure Btnx_3Click(Sender: TObject);
    procedure Btnx_4Click(Sender: TObject);
    procedure Btnx_1Click(Sender: TObject);
    procedure Btnx_5Click(Sender: TObject);
  private
    FUniConfig:TUniConfig;
    FListCard :TStringList;
  protected  
    procedure SetInitialize;override;
    procedure SetCommParams;override;
    procedure SetGridParams;override;
    procedure SetComboItems;override;
    procedure TryFreeAndNil;override;  
  public
  end;

var
  Form1: TForm1;

implementation
uses
  Class_KzUtils,Class_KzDebug,Class_AppUtil;

{$R *.dfm}

procedure TForm1.Btnx_ListDBClick(Sender: TObject);
var
  //I:Integer;
  SQLA:string;  
  NumbA:Integer;

  UniConnct:TUniConnection;
begin
  try
    UniConnct:=UniConnctEx.GetConnection(FUniConfig);
    //->
    KzDebug.TickLog('step1:');
    if FListCard=nil then
    begin
      FListCard:=TStringList.Create;
    end;
    SQLA:='select * from tbl_card';
    TKzUtils.JustCleanList(FListCard);
    TCARD.ListDB(SQLA,['CARD_ZCTM'],UniConnct,FListCard);
    KzDebug.TickLog('step2:');
    //-<
  finally
    FreeAndNil(UniConnct);
  end;
end;

procedure TForm1.SetComboItems;
begin
  inherited;
  FUniConfig:=TUniConfig.Create;
  FUniConfig.UnixType:=CONST_PROVIDER_SQLSRV;
  FUniConfig.UnixPswd:='kazarus0618';
  FUniConfig.UnixUser:='sa';
  FUniConfig.DataBase:='DBZCGL_YZXZGDZC';
  FUniConfig.UnixServ:='.\kzdb';
end;

procedure TForm1.SetCommParams;
begin
  inherited;
  RzSplitter1.Percent:=50;
end;

procedure TForm1.SetGridParams;
begin
  inherited;

  with Tree_View do
  begin
    
  end;  
end;

procedure TForm1.SetInitialize;
begin
  inherited;

end;

procedure TForm1.TryFreeAndNil;
begin
  inherited;
  FreeAndNil(FUniConfig);
end;

procedure TForm1.Btnx_2Click(Sender: TObject);
begin
  if (FListCard=nil) or (FListCard.Count=0) then Exit;

  KzDebug.TickLog('step3:');
  Tree_View.BeginUpdate;
  Tree_View.Clear;
  Tree_View.NodeDataSize :=SizeOf(TCARD);
  Tree_View.RootNodeCount:=FListCard.Count;
  Tree_View.EndUpdate;
  KzDebug.TickLog('step4:');    
end;

procedure TForm1.Tree_ViewGetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  var CellText: WideString);

var
  CardA:TCard;
begin
  CardA := TCard(FListCard.Objects[Node.Index]);
  case Column of
    0:
      CellText := CardA.UNITLINK;
    1:
      CellText := CardA.CARDNAME;
    2:
      CellText := CardA.CARDCODE;
    3:
      CellText := CardA.CARDZCTM;
    4:
      CellText := CardA.ZCLBLINK;
  else
    CellText := '';
  end;
end;


procedure TForm1.Tree_ViewInitNode(Sender: TBaseVirtualTree; ParentNode,
  Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);

{var
  CardA: PCard;}
begin

  { Note:
    Only store the minimum information you need in the
    data associated with a tree node... in most cases, this
    is simply enough data you need to display information to the user.
    other information should be looked up form the database on demand,
    using the OnClick (or whatever) events of the tree.
  }

  //CardA :=PCARD(FListCard.Objects[Node.Index]);
  
  //CardA := Sender.GetNodeData(Node);


  //Initialize(CardA^);
  //CardA.UNITLINK:='0001';
  
  {Track.PlaceName := ADOQuery.FieldByName('PlaceName').AsString;
  Track.Longitude := ADOQuery.FieldByName('Longitude').AsFloat;
  Track.Latitude := ADOQuery.FieldByName('Latitude').AsFloat;
  Track.Speed := ADOQuery.FieldByName('Speed').AsInteger;
  Track.Direction := ADOQuery.FieldByName('Direction').AsInteger;
  Track.GpsTime := ADOQuery.FieldByName('GpsTime').AsDateTime;
  Track.Checked := False;
  if not ADOQuery.Eof then
    ADOQuery.Next;
  Node.CheckType := ctCheckBox;}
  // Thats all - the actual DISPLAY of information takes place in the GETTEXT event
  Node.CheckType:=VirtualTrees.ctCheckBox;
end;


procedure TForm1.Btnx_3Click(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.Btnx_4Click(Sender: TObject);
var
  I:Integer;
  CardA:TCARD;
begin
  TAppUtil.ClearGrid(Grid_1,1);
  if (FListCard=nil) or (FListCard.Count=0) then Exit;

  KzDebug.TickLog('step5');
  with Grid_1 do
  begin
    BeginUpdate;

    TAppUtil.ClearGrid(Grid_1,FListCard.Count);

    for I:=0 to FListCard.Count-1 do
    begin
      CardA:=TCARD(FListCard.Objects[I]);
      Cells[1,I+1]:=CardA.UNITLINK;
      Cells[2,I+1]:=CardA.CARDNAME;
      Cells[3,I+1]:=CardA.CARDCODE;
      Cells[4,I+1]:=CardA.CARDZCTM;
    end;

    EndUpdate;
  end;
  KzDebug.TickLog('step6');    
end;

procedure TForm1.Btnx_1Click(Sender: TObject);
begin
  if FListCard<>nil then
  begin
    FListCard.Sorted:=True;
  end;
end;

procedure TForm1.Btnx_5Click(Sender: TObject);
var
  IdexA:Integer;
begin
  KzDebug.TickLog('X:');
  IdexA:=FListCard.IndexOf('000000800187');
  if IdexA<>-1 then
  begin
    //
  end;
  KzDebug.TickLog('Y:');
end;

end.
